from django import forms
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User
from captcha.fields import CaptchaField
from .models import Profile

class LoginForm(AuthenticationForm):
    """登录表单，包含验证码"""
    captcha = CaptchaField(label='验证码')
    
    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget.attrs.update({'class': 'form-control', 'placeholder': '请输入用户名'})
        self.fields['password'].widget.attrs.update({'class': 'form-control', 'placeholder': '请输入密码'})

class SignUpForm(UserCreationForm):
    """用户注册表单，包含验证码"""
    email = forms.EmailField(max_length=254, required=True, 
                           widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': '请输入您的电子邮件'}))
    captcha = CaptchaField(label='验证码')
    
    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2', 'captcha')
        widgets = {
            'username': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '请输入用户名'}),
        }
    
    def __init__(self, *args, **kwargs):
        super(SignUpForm, self).__init__(*args, **kwargs)
        self.fields['password1'].widget.attrs.update({'class': 'form-control', 'placeholder': '请输入密码'})
        self.fields['password2'].widget.attrs.update({'class': 'form-control', 'placeholder': '请再次输入密码'})

class ProfileForm(forms.ModelForm):
    """用户资料表单"""
    
    class Meta:
        model = Profile
        fields = ['bio', 'website']
        widgets = {
            'bio': forms.Textarea(attrs={'class': 'form-control', 'rows': 4, 'placeholder': '请简单介绍一下自己...'}),
            'website': forms.URLInput(attrs={'class': 'form-control', 'placeholder': 'https://example.com'}),
        }
        help_texts = {
            'bio': _('简单介绍一下自己，将显示在您提交的规则旁边'),
            'website': _('您的个人网站或社交媒体链接'),
        } 